package com.yuelao.thread.demo;

import java.util.concurrent.Exchanger;

/**
 * 会是两个线程的数据交换
 * Created by fengliye on 2017/10/7.
 */
public class ExchangerTest {
    private static final Exchanger<String> e = new Exchanger();

    public static void main(String[] args) {
        new Thread(() ->{
            String A = "银行流水A";
            try {
                String in = e.exchange(A);
                System.out.println("t1:" + in);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }).start();
        new Thread(() ->{
            String B = "银行流水B";
            try {
                String A = e.exchange("B");
                System.out.println("t2:" + A);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }).start();
        // 只能是两个线程间数据互换
        new Thread(() ->{
            String B = "银行流水B";
            try {
                String A = e.exchange("D");
                System.out.println("t3:" + A);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }).start();
    }
}
